package com.cesgroup.zw.spring.boot.library.echarts.barAndScatter.complex;

import com.cesgroup.zw.spring.boot.echarts.component.series.Bar;
import com.cesgroup.zw.spring.boot.echarts.enums.Position;
import com.cesgroup.zw.spring.boot.echarts.model.style.ItemStyle;
import com.cesgroup.zw.spring.boot.echarts.model.style.LabelStyle;

import java.util.Arrays;

/**
 * @author ytj
 * @date 2019/7/5 13:51
 */
public class ComplexBarSeries extends Bar{

    public ComplexBarSeries(ComplexBarAndScatterParam param,String type){
        if("条".equals(type))
            this.data(param);
        else if("白框".equals(type))
            this.whiteBox(param);
        else if("外框".equals(type))
            this.outerBox(param);
    }

    public void data(ComplexBarAndScatterParam param){
        this.setName("条");
        this.setType("bar");
        this.setYAxisIndex(0);
        this.setData((Object[]) param.getDataset().get("data"));
        this.setLabel(new LabelStyle(){{
            this.setShow(true);
            this.setPosition(Position.right);
            this.setColor("#ffffff");
            this.setFontSize(16);
        }});
        this.setBarWidth(12);
        this.setZ(2);
        this.setItemStyle(new ItemStyle(){{
            Object[] obj=(Object[]) param.getDataset().get("color");
            StringBuffer sbf=new StringBuffer();
            sbf.append("[");
            Arrays.stream(obj).forEach(e->sbf.append("'"+e+"',"));
            sbf.append("]");
            this.setColor("function(params) {var myColor="+sbf+" ;var num = myColor.length;return myColor[params.dataIndex % num]}");
        }});
    }

    public void whiteBox(ComplexBarAndScatterParam param){
        this.setName("白框");
        this.setType("bar");
        this.setYAxisIndex(1);
        this.setBarGap("-100%");
        this.setData((Object[]) param.getDataset().get("white"));
        this.setBarWidth(20);
        this.setItemStyle(new ItemStyle(){{
            this.setColor("#0e2147");
            this.setBarBorderRadius(5);
        }});
        this.setZ(1);
    }

    public void outerBox(ComplexBarAndScatterParam param){
        this.setName("外框");
        this.setType("bar");
        this.setYAxisIndex(2);
        this.setBarGap("-100%");
        this.setData((Object[])param.getDataset().get("outer"));
        this.setBarWidth(12);
        this.setItemStyle(new ItemStyle(){{
            this.setBarBorderRadius(5);
            Object[] obj=(Object[]) param.getDataset().get("color");
            StringBuffer sbf=new StringBuffer();
            sbf.append("[");
            Arrays.stream(obj).forEach(e->sbf.append("'"+e+"',"));
            sbf.append("]");
            this.setColor("function(params) {var myColor="+sbf+" ;var num = myColor.length;return myColor[params.dataIndex % num]}");
        }});
        this.setZ(0);
    }

}
